package com.itdct.onflow.core.config

import com.alibaba.fastjson2.JSONWriter
import com.alibaba.fastjson2.support.config.FastJsonConfig
import com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter
import org.springframework.boot.autoconfigure.http.HttpMessageConverters
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

/**
 * @author Zhouwx
 * @date 2025/7/19 22:48:06
 * @version 1.0
 * @description
 */
@Configuration
class JsonConfig {

    @Bean
    fun fastJsonHttpMessageConverters(): HttpMessageConverters {
        // 1.定义一个converters转换消息的对象
        val fastConverter = FastJsonHttpMessageConverter()
        // 2.添加fastjson的配置信息，比如: 是否需要格式化返回的json数据
        val fastJsonConfig = FastJsonConfig();
        // 3.在converter中添加配置信息
        fastJsonConfig.dateFormat = "yyyy-MM-dd HH:mm:ss"
        fastJsonConfig.setWriterFeatures(JSONWriter.Feature.WriteNulls, JSONWriter.Feature.PrettyFormat, JSONWriter.Feature.SortMapEntriesByKeys)
        fastConverter.fastJsonConfig = fastJsonConfig
        // 4.返回HttpMessageConverters对象
        return HttpMessageConverters(fastConverter)
    }
}